package cn.tedu.erp.project.service.impl;

import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.mapper.CustomManageMapper;
import cn.tedu.erp.project.pojo.dto.CustomManageAddNewDTO;
import cn.tedu.erp.project.pojo.dto.CustomManageUpdateDTO;
import cn.tedu.erp.project.pojo.entity.CustomAgreement;
import cn.tedu.erp.project.pojo.entity.CustomManage;
import cn.tedu.erp.project.pojo.entity.CustomSales;
import cn.tedu.erp.project.pojo.vo.CustomAgreementStandardVO;
import cn.tedu.erp.project.pojo.vo.CustomManageListVO;
import cn.tedu.erp.project.pojo.vo.CustomManageStandardVO;
import cn.tedu.erp.project.pojo.vo.CustomSalesListVO;
import cn.tedu.erp.project.service.ICustomManageService;
import cn.tedu.erp.project.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CustomManageServiceImpl implements ICustomManageService {

    @Autowired
    private CustomManageMapper customManageMapper;
    @Override
    public void addNew(CustomManageAddNewDTO customSalesAddNewDTO) {
        log.debug("开始处理【添加客户】的业务,参数: {}",customSalesAddNewDTO);
        // 调用参数对象的getName()得到尝试添加的客户的名称
        String name = customSalesAddNewDTO.getCompanyName();
        // 调用Mapper对象的countByName()执行统计查询
        int count = customManageMapper.countByCompanyName(customSalesAddNewDTO.getCompanyName());
        log.debug("根据客户id统计数量:{}",count);
        // 判断统计结果是否大于0
        if (count > 0){
            // -- 是：名称被占用，抛出异常，例如：throw new RuntimeException()
            String message = "此客户已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        // 创建订单对象
        CustomManage customManage = new CustomManage();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到customSales对象中
        BeanUtils.copyProperties(customSalesAddNewDTO,customManage);
        // 调用Mapper对象的insert()执行插入客户数据
        log.debug("插入数据:{}",customManage);
        customManageMapper.insert(customManage);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除客户】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        CustomManageStandardVO customAgreement=customManageMapper.getStandardById(id);
        log.debug("根据ID={}检查客户数据是否存在，查询结果：{}", id, customAgreement);
        // 判断查询结果是否为null
        if (customAgreement == null) {
            // 是：数据不存在，抛出异常
            String message = "删除客户失败，尝试删除的客户数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        customManageMapper.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, CustomManageUpdateDTO customManageUpdateDTO) {
        log.debug("开始处理【修改客户详情】的业务，ID：{}，新数据：{}", id, customManageUpdateDTO);
        // 调用Mapper对象的getStandardById()执行查询
        CustomManageStandardVO queryResult = customManageMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改客户详情失败，尝试修改的客户数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的countByNameAndNotId()执行统计
        int count = customManageMapper.countByNameAndNotId(customManageUpdateDTO.getCompanyName(),id);
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "修改客户详情失败，客户名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建WareHouseCategory对象
        CustomManage customManage= new CustomManage();
        // 复制属性，设置ID
        BeanUtils.copyProperties(customManageUpdateDTO, customManage);
        customManage.setId(id);
        // 调用Mapper对象的update()方法执行修改
        customManageMapper.update(customManage);
    }

    @Override
    public CustomManageStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询客户详情】的业务，参数：{}", id);

        CustomManageStandardVO customManage= customManageMapper.getStandardById(id);
        if (customManage == null) {
            // 是：抛出异常
            String message = "查询客户详情失败，客户数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return customManage;
    }

    @Override
    public List<CustomManageListVO> list() {
        log.debug("开始处理【客户列表】的业务,无参数");
        List<CustomManageListVO> list = customManageMapper.list();
        return list;
    }

    @Override
    public void rebuildCache() {

    }
}
